package com.example.unlock2

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.widget.ImageView
import com.example.unlock2.databinding.ActivityMainBinding

class RecallActivity : AppCompatActivity(),IUnlockView {
    private lateinit var dotArray:List<ImageView>
 private lateinit var binding :ActivityMainBinding
    private lateinit var presenter:UnlockPresenter
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val modelsArray = arrayListOf<ImageViewModel>()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        //保存9个点
        dotArray = listOf(binding.dot1,binding.dot2,binding.dot3,binding.dot4,binding.dot5,binding.dot6,binding.dot7,binding.dot8,binding.dot9)
        dotArray.forEach {
           modelsArray.add(ImageViewModel(it,R.drawable.dot_normal,R.drawable.dot_selected))
        }
        //竖线
        val verticalLineArray = arrayOf(binding.line14, binding.line25,binding.line36,binding.line47,binding.line58,binding.line69)
        verticalLineArray.forEach {
           modelsArray.add(ImageViewModel(it,R.drawable.line_1_normal,R.drawable.line_1_error))
        }
        //横线
        val landscapeLineArray = arrayOf(binding.line12,binding.line23,binding.line45,binding.line56,binding.line78,binding.line89)
        landscapeLineArray.forEach {
           modelsArray.add(ImageViewModel(it,R.drawable.line_2_normal,R.drawable.line_2_error))
        }
        //左斜
        val leftSlashLineArray = arrayOf(binding.line24, binding.line35, binding.line57,binding.line68)
        leftSlashLineArray.forEach {
           modelsArray.add(ImageViewModel(it,R.drawable.line_4_normal,R.drawable.line_4_error))
        }
        //右斜
        val rightSlashLineArray = arrayOf(binding.line15, binding.line26,binding.line48, binding.line59)
        rightSlashLineArray.forEach {
          modelsArray.add(ImageViewModel(it,R.drawable.line_3_normal,R.drawable.line_3_error))
        }
        val mLineTagsList:List<Int> = listOf(12,23,45,56,78,89, 14,25,36,47,58,69, 15,26,48,59,24,35,57,68)
        presenter = UnlockPresenter(this,dotArray,modelsArray,mLineTagsList)
        //用户登录信息
        presenter.loadUserInfo()
    //给容器添加触摸事件
        binding.container.setOnTouchListener { v, event ->
            val x = event.x
            val y = event.y
          when(event.action){
              MotionEvent.ACTION_DOWN ->presenter.userTouch(x,y)
              MotionEvent.ACTION_MOVE ->presenter.userTouch(x,y)
              MotionEvent.ACTION_UP ->presenter.endTouch()
          }
            true
           }
        }
    override fun showAlertText(text: String) {
        binding.alertTitle.text = text
    }

    @SuppressLint("SuspiciousIndentation")
    override fun switchActivity() {
    //界面切换
    val init = Intent(this,HomeActivity::class.java)
        startActivity(init)
    }
}



